package com.bnz.search.listener;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.BulkRequest;
import com.alibaba.fastjson.JSON;
import com.bnz.search.entity.ItemEntity;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
//  审核监听
@Component
@RabbitListener(queues = "rmb-updateStatus")
public class MyUpdateStatusltemListener {
    @Autowired
    ElasticsearchClient client;
    @RabbitHandler
    public void getItemsInfo(String info) throws IOException {
        //将得到的字符串转换为List集合
        List<ItemEntity> itemEntities = JSON.parseArray(info, ItemEntity.class);
        //将此数据添加到索引库中
        //将商品放到索引库中  ---审核通过的商品。
        BulkRequest.Builder br = new BulkRequest.Builder();
        for (ItemEntity item : itemEntities) {
            br.operations(option->option
                    .index(idx->idx
                            .index("item")
                            .id(item.getId()+"")
                            .document(item)));
        }
        BulkRequest build = br.build();
        client.bulk(build);

        //提示信息
        System.out.println("添加经过审核通过的商品到索引库成功！");
    }
}
